<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-07-27
 * Time: 13:59
 */
$i = $_GET['pn'] ? $_GET['pn'] : 0;
$k = !empty($_GET['kw']) ? $_GET['kw'] : '男科医院';
session_start();
$_SESSION['key'] = $k;

$headers = randIP();
$curl = curl_init();
// 设置你需要抓取的URL
$url = "http://m.baidu.com/ssid=f2247777775f7a7a3631385f636f6d6b2b/s?iscookie=1&word={$_SESSION['key']}&ts=4589965&t_kt=0&ie=utf-8&rsv_iqid=3004532905&rsv_t=b2deAWZTCNduxxdvzWu0jQ1zbCnWtMXhb%252B7nXZoZMiwJWNWCTLZM&sa=ib&rsv_pq=3004532905&rsv_sug4=4023&ss=110&tj=1&inputT=719&pn={$i}";
curl_setopt($curl, CURLOPT_URL, $url);
// 设置header
curl_setopt($curl, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4'));
// 设置cURL 参数，要求结果保存到字符串中还是输出到屏幕上。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); //不验证证书
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); //不验证证书
// 运行cURL，请求网页
$data = curl_exec($curl);
$info = cdomain($data);
$info = preg_replace('/<div class="ec_wise_ad">.*?<\/p>(\t|\r\n|\n|\s)*<\/div>/ism'," ",$info);
// $info = preg_replace('/<div class="ec_ad_results">.*?<\/p>(\t|\r\n|\n|\s)*<\/div>/is',"",$info);


curl_close($curl);
echo $info;


//图片域名替换
function cdomain($str) {
	$baidu_domain = '{
"http://graph.baidu.com": "https://sp0.baidu.com/-aYHfD0a2gU2pMbgoY3K", "http://p.qiao.baidu.com":"https://sp0.baidu.com/5PoXdTebKgQFm2e88IuM_a", "http://vse.baidu.com":"https://sp3.baidu.com/6qUDsjip0QIZ8tyhnq", "http://hdpreload.baidu.com":"https://sp3.baidu.com/7LAWfjuc_wUI8t7jm9iCKT-xh_", "http://lcr.open.baidu.com":"https://sp2.baidu.com/8LUYsjW91Qh3otqbppnN2DJv", "http://kankan.baidu.com":"https://sp3.baidu.com/7bM1dzeaKgQFm2e88IuM_a", "http://xapp.baidu.com":"https://sp2.baidu.com/yLMWfHSm2Q5IlBGlnYG", "http://dr.dh.baidu.com":"https://sp0.baidu.com/-KZ1aD0a2gU2pMbgoY3K", "http://xiaodu.baidu.com":"https://sp0.baidu.com/yLsHczq6KgQFm2e88IuM_a", "http://sensearch.baidu.com":"https://sp1.baidu.com/5b11fzupBgM18t7jm9iCKT-xh_", "http://s1.bdstatic.com":"https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K", "http://olime.baidu.com":"https://sp0.baidu.com/8bg4cTva2gU2pMbgoY3K", "http://app.baidu.com":"https://sp2.baidu.com/9_QWsjip0QIZ8tyhnq", "http://i.baidu.com":"https://sp0.baidu.com/74oIbT3kAMgDnd_", "http://c.baidu.com":"https://sp0.baidu.com/9foIbT3kAMgDnd_", "http://sclick.baidu.com":"https://sp0.baidu.com/5bU_dTmfKgQFm2e88IuM_a", "http://nsclick.baidu.com":"https://sp1.baidu.com/8qUJcD3n0sgCo2Kml5_Y_D3", "http://sestat.baidu.com":"https://sp1.baidu.com/5b1ZeDe5KgQFm2e88IuM_a", "http://eclick.baidu.com":"https://sp3.baidu.com/-0U_dTmfKgQFm2e88IuM_a", "http://api.map.baidu.com":"https://sp2.baidu.com/9_Q4sjOpB1gCo2Kml5_Y_D3", "http://ecma.bdimg.com":"https://ss1.bdstatic.com/-0U0bXSm1A5BphGlnYG", "http://ecmb.bdimg.com":"https://ss0.bdstatic.com/-0U0bnSm1A5BphGlnYG", "http://t1.baidu.com":"https://ss0.baidu.com/6ON1bjeh1BF3odCf", "http://t2.baidu.com":"https://ss1.baidu.com/6OZ1bjeh1BF3odCf", "http://t3.baidu.com":"https://ss2.baidu.com/6OV1bjeh1BF3odCf", "http://t10.baidu.com":"https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq", "http://t11.baidu.com":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq", "http://t12.baidu.com":"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq", "http://i7.baidu.com":"https://ss0.baidu.com/73F1bjeh1BF3odCf", "http://i8.baidu.com":"https://ss0.baidu.com/73x1bjeh1BF3odCf", "http://i9.baidu.com":"https://ss0.baidu.com/73t1bjeh1BF3odCf", "http://b1.bdstatic.com":"https://ss0.bdstatic.com/9uN1bjq8AAUYm2zgoY3K", "http://ss.bdimg.com":"https://ss1.bdstatic.com/5aV1bjqh_Q23odCf", "http://opendata.baidu.com":"https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv", "http://api.open.baidu.com":"https://sp0.baidu.com/9_Q4sjW91Qh3otqbppnN2DJv", "http://tag.baidu.com":"https://sp1.baidu.com/6LMFsjip0QIZ8tyhnq", "http://f3.baidu.com":"https://sp2.baidu.com/-uV1bjeh1BF3odCf", "http://s.share.baidu.com":"https://sp0.baidu.com/5foZdDe71MgCo2Kml5_Y_D3", "http://bdimg.share.baidu.com":"https://ss1.baidu.com/9rA4cT8aBw9FktbgoI7O1ygwehsv", "http://1.su.bdimg.com":"https://ss0.bdstatic.com/k4oZeXSm1A5BphGlnYG", "http://2.su.bdimg.com":"https://ss1.bdstatic.com/kvoZeXSm1A5BphGlnYG", "http://3.su.bdimg.com":"https://ss2.bdstatic.com/kfoZeXSm1A5BphGlnYG", "http://4.su.bdimg.com":"https://ss3.bdstatic.com/lPoZeXSm1A5BphGlnYG", "http://5.su.bdimg.com":"https://ss0.bdstatic.com/l4oZeXSm1A5BphGlnYG", "http://6.su.bdimg.com":"https://ss1.bdstatic.com/lvoZeXSm1A5BphGlnYG", "http://7.su.bdimg.com":"https://ss2.bdstatic.com/lfoZeXSm1A5BphGlnYG", "http://8.su.bdimg.com":"https://ss3.bdstatic.com/iPoZeXSm1A5BphGlnYG" }
';
	$domain = json_decode($baidu_domain, TRUE);
	foreach ($domain as $k => $v) {
		$str = str_replace($k, $v, $str);
	}
	return $str;
}


//此函数提供了国内的IP地址
function randIP(){
	$ip_long = array(
		array('607649792', '608174079'), //36.56.0.0-36.63.255.255
		array('1038614528', '1039007743'), //61.232.0.0-61.237.255.255
		array('1783627776', '1784676351'), //106.80.0.0-106.95.255.255
		array('2035023872', '2035154943'), //121.76.0.0-121.77.255.255
		array('2078801920', '2079064063'), //123.232.0.0-123.235.255.255
		array('-1950089216', '-1948778497'), //139.196.0.0-139.215.255.255
		array('-1425539072', '-1425014785'), //171.8.0.0-171.15.255.255
		array('-1236271104', '-1235419137'), //182.80.0.0-182.92.255.255
		array('-770113536', '-768606209'), //210.25.0.0-210.47.255.255
		array('-569376768', '-564133889'), //222.16.0.0-222.95.255.255
	);
	$rand_key = mt_rand(0, 9);
	$ip= long2ip(mt_rand($ip_long[$rand_key][0], $ip_long[$rand_key][1]));
	$headers['CLIENT-IP'] = $ip;
	$headers['X-FORWARDED-FOR'] = $ip;

	$headerArr = array();
	foreach( $headers as $n => $v ) {
		$headerArr[] = $n .':' . $v;
	}
	return $headerArr;
}